package br.unipampa.utilitarios;

import br.unipampa.dados.DBCompromisso;
import br.unipampa.dados.DBConection;
import br.unipampa.view.FormularioPrincipal;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Gabriel Moro
 * @since 15/03/2013
 * @version 1.2
 */
public class ThreadTimer extends Thread {

    private Date data;
    private DateFormat dateFormat;
    private FormularioPrincipal form;
    private TrataDataHora trataData;
    private TrataAlarme trataAlarme;
    private DBConection conexao;
    private DBCompromisso dbCompromisso;
    
    public ThreadTimer(FormularioPrincipal form) {
        this.form = form;
        this.dateFormat = new SimpleDateFormat("HH:mm:ss");
    }

    @Override
    public void run() {
        try {
            //DATA
            this.trataData = new TrataDataHora();
            //ALARME
            this.trataAlarme = TrataAlarme.getInstance();

            while (true) {
                this.data = new Date();
                String text = this.dateFormat.format(data);
                ArrayList<Object> tempo = this.trataData.retornaTempoAtual();
                String dia = Utilitaria.converteParaTexto(tempo.get(0));
                String mes = Utilitaria.converteParaTexto(tempo.get(1));
                String ano = Utilitaria.converteParaTexto(tempo.get(2));

                String data = dia + "/" + mes + "/" + ano;
                this.form.atualizaPanelTimer(text + " AM", data);
                
                /*EM CONSTRUÇÂO*/
                if (!data.equalsIgnoreCase(this.trataAlarme.getData())) {
                    this.trataAlarme.setData(data);
                    try {
                        this.conexao = new DBConection();
                        this.dbCompromisso = new DBCompromisso(this.conexao.getCon());
                        ArrayList<Object> valores = this.dbCompromisso.selected("definicao_compromisso,codAno_compromisso,codMes_compromisso,codDia_compromisso,codHora_compromisso", Utilitaria.converteParaInteiro(dia), mes, ano);
                        for(int x=0;x<valores.size();x+=2){
                            this.trataAlarme.addHorarioCompromisso(Utilitaria.converteParaTexto(valores.get(x)), Utilitaria.converteParaTexto(valores.get(x+1)));
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(ThreadTimer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                /*EM CONSTRUÇÂO*/
                this.trataAlarme.monitorar(Utilitaria.quebrarHora(text));
                ThreadTimer.sleep(1000);
            }
        } catch (InterruptedException erro1) {
            erro1.printStackTrace();
        } catch (NullPointerException erro2) {
            erro2.printStackTrace();
        }
    }
}
